package com.liwq.base.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2023/1/31
 **/
public class CompletableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("开启子线程");
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            return 2;
//        });
//        System.out.println(completableFuture.whenComplete((t, u) -> {
//            System.out.println("获取到结果");
//        }).get());


        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("开启子线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 2;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println(v);
            }
        });
        System.out.println("主线程继续");
        Thread.sleep(3000);
    }
}
